package com.dtw.ditouwang.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.EditorChooseBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.WebActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MoreFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {
    List<Fragment> fragmentList = new ArrayList<>();
    RadioButton editorChoose, forYou, news;
    ViewPager viewPager;
    int type = 1;
    MoreFragmentAdapter fragmentAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        editorChoose = view.findViewById(R.id.radio_editor_choose);
        forYou = view.findViewById(R.id.radio_for_you);
        news = view.findViewById(R.id.radio_news);
        editorChoose.setOnCheckedChangeListener(this);
        forYou.setOnCheckedChangeListener(this);
        news.setOnCheckedChangeListener(this);
        viewPager = view.findViewById(R.id.viewpager);
        onCheckedChanged(editorChoose, true);
        fragmentList.clear();
        fragmentList.add(new MoreEditChooseFragment());
        fragmentList.add(new MoreForYouFragment());
        fragmentList.add(new MoreNewsFragmment());
        fragmentAdapter = new MoreFragmentAdapter(getChildFragmentManager(), fragmentList);
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(fragmentAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                Log.i("dtw",i+"position");
                if (i == 0 && !editorChoose.isChecked()) {
                    editorChoose.setChecked(true);
                } else if (i == 1 && !forYou.isChecked()) {
                    forYou.setChecked(true);
                }else if(i==2&&!news.isChecked()){
                    news.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.radio_editor_choose:
                    type = 1;
                    if(viewPager.getCurrentItem()!=0) {
                        viewPager.setCurrentItem(0);
                    }
                    break;
                case R.id.radio_for_you:
                    type = 2;
                    if(viewPager.getCurrentItem()!=1) {
                        viewPager.setCurrentItem(1);
                    }
                    break;
                case R.id.radio_news:
                    type = 3;
                    if(viewPager.getCurrentItem()!=2) {
                        viewPager.setCurrentItem(2);
                    }
                    break;
            }
        }
    }
}
